from typing import List
import random

# 绝对导入
from app.models.schemas import ChatMessage


class MockAIModels:
    """
    Mock AI models for development and testing
    """

    def __init__(self):
        self.model_responses = {
            "gpt-3.5-turbo": [
                "As an AI assistant, I'm here to help you with your questions and tasks.",
                "I understand your query and will do my best to provide a helpful response.",
                "That's an interesting question. Let me think about how to approach this.",
                "Based on the information provided, I can offer the following insights."
            ],
            "gpt-4": [
                "As a more advanced AI model, I can provide more detailed and nuanced responses to your queries.",
                "I've analyzed your question from multiple perspectives and here's my comprehensive response.",
                "This is a complex topic that requires careful consideration. Here's my detailed analysis.",
                "Drawing from extensive training data, I can offer sophisticated insights on this matter."
            ],
            "claude-3-sonnet": [
                "As Claude, I approach problems with careful reasoning and attention to detail.",
                "I've considered your question thoroughly and here's my thoughtful response.",
                "This seems like an important topic that deserves a well-reasoned answer.",
                "Let me break down this problem systematically to provide the best possible solution."
            ],
            "llama-2-7b": [
                "As an open-source language model, I'll do my best to help with your query.",
                "Here's what I think about your question based on my training.",
                "I may not have all the answers, but I'll try to provide a helpful response.",
                "Let me share my perspective on this topic."
            ]
        }

        self.default_responses = [
            "I understand your message and I'm here to assist you.",
            "Thank you for your query. How can I help you today?",
            "I've received your message and I'm processing your request.",
            "Let me know how I can assist you with your questions or tasks."
        ]

    def get_response(
            self,
            messages: List[ChatMessage],
            model: str,
            temperature: float = 0.7
    ) -> str:
        """
        Get a mock response based on the model and conversation history
        """
        # Get model-specific responses or use default
        responses = self.model_responses.get(model, self.default_responses)

        # Add some randomness based on temperature
        if temperature > 0.8:
            # High temperature - more creative/random responses
            response_pool = responses + self._get_creative_responses()
        elif temperature < 0.3:
            # Low temperature - more deterministic
            response_pool = responses[:2] if len(responses) > 2 else responses
        else:
            response_pool = responses

        # Select random response
        response = random.choice(response_pool)

        # Add context from last user message if available
        user_messages = [msg for msg in messages if msg.role == "user"]
        if user_messages:
            last_user_message = user_messages[-1].content
            if len(last_user_message) < 50:  # Only for short messages
                response = f"Regarding '{last_user_message}', {response.lower()}"

        return response

    def _get_creative_responses(self) -> List[str]:
        """Get additional creative responses for high temperature"""
        return [
            "That's a fascinating perspective! Let me explore this with you.",
            "I sense there's more to this question than meets the eye. Let's dive deeper.",
            "What an intriguing query! This reminds me of several related concepts.",
            "I appreciate you bringing this up. It connects to some interesting ideas I've been considering."
        ]